package cn.xiuxius.askbox.controller.user;


import cn.xiuxius.askbox.common.constant.AnswerState;
import cn.xiuxius.askbox.common.constant.DataState;
import cn.xiuxius.askbox.common.context.AppContext;
import cn.xiuxius.askbox.pojo.dto.add.AskAddDTO;
import cn.xiuxius.askbox.pojo.dto.query.AskQueryDTO;
import cn.xiuxius.askbox.pojo.result.PageResult;
import cn.xiuxius.askbox.pojo.result.Result;
import cn.xiuxius.askbox.pojo.vo.AskVO;
import cn.xiuxius.askbox.service.IAskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxiu
 * @since 2024-07-20
 */
@Api(tags = "用户-提问接口")
@RestController("userAskController")
@RequestMapping("/user/asks")
public class AskController {

    @Autowired
    private IAskService askService;

    @ApiOperation("提交问题")
    @PostMapping
    public Result submitAsk(@Validated @RequestBody AskAddDTO dto) {
        askService.submitAsk(dto);
        return Result.success();
    }


    @ApiOperation("分页查询问题")
    @GetMapping
    public Result getAsksByPage(@Validated AskQueryDTO queryDTO) {
        queryDTO.setUserUuid(AppContext.getUserUuid());
        queryDTO.setState(DataState.ALIVE);
        PageResult pageResult = askService.getAsksByPage(queryDTO);
        for (Object row : pageResult.getRows()) { //数据脱敏 匿名
            if (!(row instanceof AskVO askVO)) continue;
            askVO.setIp(null);
            askVO.setReplyEmail(null);
            askVO.setAgent(null);
            askVO.setState(null);
            askVO.setUpdateTime(null);
        }
        return Result.success(pageResult);
    }

    @ApiOperation("分页查询其他用户问题")
    @GetMapping("/{uuid}")
    public Result getOtherUserAsksByPage(@Validated AskQueryDTO dto,@PathVariable String uuid) {
        dto.setUserUuid(uuid);
        dto.setState(DataState.ALIVE);
        dto.setAnswerState(AnswerState.REPLIED);
        PageResult pageResult = askService.getAsksByPage(dto);
        for (Object row : pageResult.getRows()) { //数据脱敏 匿名
            if (!(row instanceof AskVO askVO)) continue;
            askVO.setAgent(null);
            askVO.setReplyEmail(null);
            askVO.setIp(null);
            askVO.setUpdateTime(null);
        }
        return Result.success(pageResult);
    }
}
